﻿using Grpc.Core;
using GrpcPtlSvr;
using Gxrtbtc.Ptl;
using Microsoft.Extensions.Logging;
using PtlSvrPrj;
using System.Threading.Tasks;

namespace PtlSvr
{
    /*
     * 采集服务状态获取接口
     */
    #region snippet
    public class GRPCSvr : GrpcPtlSvr.SvrGrpc.SvrGrpcBase
    {
        //数据缓存
        private static ReplyLibs replyLibs = new ReplyLibs();
        private static ReplyConnStatus replyConnStatus = new ReplyConnStatus() { TerminalData = new TerminalData() };
        private static ReplyTerminalInfo replyTerminalInfo = new ReplyTerminalInfo();
        private static TerminalFilterModel terminalFilterModel = new TerminalFilterModel();

        private readonly ILogger<GRPCSvr> _logger;
        public GRPCSvr(ILogger<GRPCSvr> logger)
        {
            _logger = logger;
        }

        /*
         * 获取载入的解析库列表
         */
        public override Task<ReplyLibs> GetPtlLibs(RequestModel request, ServerCallContext context)
        {
            replyLibs.Libs.Clear();
            replyLibs.Libs.Add(((PtlNetServer)Program._server).ptlLoader.ptlLibs);
            return Task.FromResult(replyLibs);
        }

        /*
         * 获取终端连接状态
         * 连接数，接收包总数，接收包速率，出错记录数
         */
        public override Task<ReplyConnStatus> GetConnStatus(RequestModel request, ServerCallContext context)
        {
            replyConnStatus.ConnCount = ulong.Parse(((PtlNetServer)Program._server).terminalInfos.Count.ToString());
            replyConnStatus.TerminalData.RxPackCount = ((PtlNetServer)Program._server).RxPackCount;
            replyConnStatus.TerminalData.RxPackRate = ((PtlNetServer)Program._server).RxPackRate;
            replyConnStatus.TerminalData.RxErrPackCount = ((PtlNetServer)Program._server).RxErrPackCount;
            return Task.FromResult(replyConnStatus);
        }

        /*
         * 获取在用的所有终端的ip，端口等
         */
        public override Task<ReplyTerminalInfo> GetConnInfo(RequestModel request, ServerCallContext context)
        {
            //有带参数，则返回参数所请求的信息
            if (request != null && request.Args != null && !"".Equals(request.Args) && ((PtlNetServer)Program._server).terminalInfosAll.ContainsKey(request.Args))
            {
                replyTerminalInfo.TerminalStatus.Add(((PtlNetServer)Program._server).terminalInfosAll[request.Args]);
            }
            else
            { 
                replyTerminalInfo.TerminalStatus.Add(((PtlNetServer)Program._server).terminalInfos.Values);
            }
            return Task.FromResult(replyTerminalInfo);
        }

        /*
         * 设置过滤终端列表
         */
        public override Task<ReplyModel> SetTerminalFilters(TerminalFilterModel request, ServerCallContext context)
        {
            ReplyModel replyModel = new ReplyModel();
            try
            {
                terminalFilterModel = request;
                ((PtlNetServer)Program._server).filters.Clear();
                foreach (TerminalInfo terminalInfo in request.Terminalinfo)
                {
                    ((PtlNetServer)Program._server).filters.Add(terminalInfo.Ip + ":" + terminalInfo.Port, 0);
                }
                replyModel.Rs = true;
            }
            catch
            {
                replyModel.Rs = false;
            }
            return Task.FromResult(replyModel);
        }

        /*
         * 获取过滤终端列表
         */
        public override Task<TerminalFilterModel> GetTerminalFilters(RequestModel request, ServerCallContext context)
        {
            return Task.FromResult(terminalFilterModel);
        }
    }
    #endregion
}
